<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of NotificationController
 *
 * @author Admin
 */
class House_NotificationController extends Zend_Controller_Action {

    public function init() {
        /* Initialize action controller here */
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            $this->_redirect('/user/login');
        }

        $view = new Zend_View();
        $view->headTitle(' > House Digging > Notifications');
        $this->view->userDetail = $auth->getIdentity();
    }

    public function indexAction() {
        $form = new Application_Form_cabinet_TidsForm();
        $switchModel = new Application_Model_SwitchModel();
        $cabinetModel = new Application_Model_CabinetModel();
        $cabinets = $cabinetModel->getAllCabinets($this->_request);
        $cabinetId = 1;
        foreach ($cabinets as $item) {
            $cabinetId = $item['id'];
            continue;
        }
        $switches = $switchModel->getAllClosedSwitchesForHouse($this->_request, $cabinetId);
        $form->populate(array('cabinetlist' => $cabinetId));
        
        $this->view->paginator = $switches;
        $this->view->form = $form;
    }

    public function listAction() {
        $this->_helper->layout->disableLayout();
        $cabinetId = $this->_request->getParam('id');
        $switchModel = new Application_Model_SwitchModel();

        $switches = $switchModel->getAllClosedSwitchesForHouse($this->_request, $cabinetId);

        $this->view->paginator = $switches;
    }

    public function addAction() {
        $switchId = $this->_request->getParam('id');
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_housedigging_AddForm();

        if ($this->_request->isPost()) {
            $team = $this->_request->getPost('team');
            $users = explode(',', $team);
            $switchUsersModel = new Application_Model_HouseTeamModel();
            $switchUsersModel->saveSwitchUsers($switchId, $users);
        }
        $this->view->form = $form;
    }

    public function toggleAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);

        $switchModel = new Application_Model_SwitchModel();

        $switchId = $this->_request->getParam('id');
        $status = $this->_request->getpost('status');
        $data = array(
            'id' => $switchId,
            'house_status' => ($status == '1') ? '0' : '1'
        );
        $switchModel->save($data);
    }

    public function filesAction() {
        $this->_helper->layout->disableLayout();
        $form = new Application_Form_documents_AddForm();
        $documentModel = new Application_Model_DocumentModel();
        $this->view->form = $form;
        $switchId = $this->_request->getParam('id');

        if ($this->_request->isPost()) {

            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $uploaded = move_uploaded_file($_FILES["doc_path"]["tmp_name"], "./uploads/files/" . $_FILES["doc_path"]["name"]);
                if ($uploaded) {
                    $data = array(
                        'name' => $this->_request->getPost('name'),
                        'file' => $_FILES["doc_path"]["name"],
                        'uploader_id' => $this->view->userDetail->id,
                        'switch' => $this->_request->getPost('switch'),
                        'status' => 'active',
                        'date' => Zend_Date::now()->toString('yyyy-MM-dd HH:mm:ss')
                    );
                    $documentModel->save($data);
                    $this->_redirect("house/notification");
                    exit;
                }
            }
        } else {
            $docs = $documentModel->getAllDocument('switch', $switchId);
            $this->view->files = $docs;
            $this->view->switchId = $switchId;
            $this->view->cabinetId = $cabinetId;
        }
    }

    public function housesAction() {
        $this->_helper->layout->disableLayout();
        $switchId = $this->_request->getParam('id');
        $houseModel = new Application_Model_HouseModel();
        $switchUserModel = new Application_Model_SwitchUserModel();

        $users = $switchUserModel->getAllHouseUser($switchId);
        $houses = $houseModel->getAllHouseDetail('switch', $switchId);

        $this->view->users = $users;
        $this->view->houses = $houses;
    }

    public function assignhousesAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $houseId = $this->_request->getParam('id');
        $userId = $this->_request->getParam('page');

        $houseModel = new Application_Model_HouseModel();

        $check = $this->_request->getPost('check');
        if ($check) {
            $houseModel->update(array('house_digging_user_id' => $userId), "id = '$houseId'");
        } else {
            $houseModel->update(array('house_digging_user_id' => 0), "id = '$houseId'");
        }
    }

}